/*
	@file : benchmark.cpp
	@author : Wolfhead
*/
#include <iostream>
#include <fstream>
#include "logger/Log.h"
#include "logger/Builder.h"
#include <boost/thread.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/thread/tss.hpp>
#include <boost/lexical_cast.hpp>

using namespace std;
using namespace logger;


typedef boost::shared_ptr<boost::thread> ThreadPtr;

void CLoggingThread(int count)
{
	while (--count)
	{
		CLOGS(kDebug) << "0123456789";
	}
}


void FLoggingThread(int count)
{
	while (--count)
	{
		CLOGF(kDebug, "0123456789");
	}
}

void FileOutputThread(int count)
{
	ofstream ofs;
	ofs.open("./test.log");

	while (--count)
	{
		ofs << "[2012-10-27 08:32:09.837916][Debug] 0123456789 (/home/wolfhead/dev/liblogger/source/LogBuilder.cpp:193)(int logger::LogBuilder::load_runner(const string&, boost::property_tree::ptree&))\n";
	}	

	ofs.close();
}

int main(int argc, char * argv[])
{
	if (argc != 3)
	{
		printf("usage: Benchmark [threadCount] [logCount]\n");
		return -1;
	}

	int thread_count = boost::lexical_cast<int>(argv[1]);
	int log_count = boost::lexical_cast<int>(argv[2]);

	{
		OStreamLoggerScope scope_logger(kAll, std::cerr);

		std::string path = "./benchmark.json";
		std::string format = "json";

		int result = get_log_builder().build_from_file(path, format);
		if (result != 0)
		{
			return -1;
		}

		get_log_dispatcher().start();
	}

	boost::posix_time::ptime start_time = boost::posix_time::microsec_clock::local_time();

	std::vector<ThreadPtr> thread_pool;
	for (int i = 0; i != thread_count; ++i)
	{
		thread_pool.push_back(ThreadPtr(new boost::thread(
			boost::bind(FLoggingThread, log_count))));
	}

	for (int i = 0; i != thread_count; ++i)
	{
		thread_pool[i]->join();
	}

	thread_pool.clear();

	boost::posix_time::ptime end_time = boost::posix_time::microsec_clock::local_time();

	printf("time : %ld\n", (end_time - start_time).total_microseconds());

	return 0;
}

